package com.store.pet.petstore.activity;

import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.blankj.utilcode.util.BarUtils;
import com.gyf.barlibrary.ImmersionBar;
import com.store.pet.petstore.bean.UserBean;
import com.store.pet.petstore.util.Constant;
import com.store.pet.petstore.util.SharedPreferencesUtil;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 基类Activity
 */
public abstract class BaseActivity extends AppCompatActivity implements  View.OnClickListener,TextWatcher,
        BottomNavigationBar.OnTabSelectedListener{

    private Unbinder bind;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(getLayoutID());
        bind = ButterKnife.bind(this);
        init(savedInstanceState);
        loadData();
    }

    /**
     * 顶替系统状态栏达到沉浸式效果
     *
     * @param topBar
     */
    protected void takeLinearStatusBar(View topBar) {
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams)
                topBar.getLayoutParams();
        layoutParams.height = BarUtils.getStatusBarHeight();
        topBar.setLayoutParams(layoutParams);
    }

    protected void takeRelativeStatusBar(View topBar) {
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)
                topBar.getLayoutParams();
        layoutParams.height = BarUtils.getStatusBarHeight();
        topBar.setLayoutParams(layoutParams);
    }

    public abstract int getLayoutID();

    /**
     * 加载布局后执行该方法初始化类
     *
     * @param savedInstanceState
     * @return
     */
    public abstract void init(Bundle savedInstanceState);

    /**
     * 加载布局后执行该方法加载数据
     *
     * @return
     */
    public abstract void loadData();

    /**
     * 返回用户角色
     */
    protected int getUserRole(){
        if(SharedPreferencesUtil.getBeanFromSp(this,Constant.FILE_NAME,Constant.DATA) != null){
            UserBean bean = SharedPreferencesUtil.getBeanFromSp(this,Constant.FILE_NAME,Constant.DATA);
            return bean.getContent().getRole();
        }
        return 0;
    }

    /**
     * 返回用户
     */
    protected int getMyId(){
        if(SharedPreferencesUtil.getBeanFromSp(this,Constant.FILE_NAME,Constant.DATA) != null){
            UserBean bean = SharedPreferencesUtil.getBeanFromSp(this,Constant.FILE_NAME,Constant.DATA);
            return bean.getContent().getId();
        }
        return 0;
    }

    protected void hideKeyboard(View view){
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0); //强制隐藏键盘
    }

//    protected ViewTreeObserver.OnGlobalLayoutListener getGlobalLayoutListener(final View decorView, final View contentView) {
//        return new ViewTreeObserver.OnGlobalLayoutListener() {
//            @Override
//            public void onGlobalLayout() {
//                Rect r = new Rect();
//                decorView.getWindowVisibleDisplayFrame(r);
//
//                int height = decorView.getContext().getResources().getDisplayMetrics().heightPixels;
//                int diff = height - r.bottom;
//
//                if (diff != 0) {
//                    if (contentView.getPaddingBottom() != diff) {
//                        contentView.setPadding(0, 0, 0, diff);
//                    }
//                } else {
//                    if (contentView.getPaddingBottom() != 0) {
//                        contentView.setPadding(0, 0, 0, 0);
//                    }
//                }
//            }
//        };
//    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
        bind.unbind();
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {

    }

    @Override
    public void onTabSelected(int position) {

    }

    @Override
    public void onTabUnselected(int position) {

    }

    @Override
    public void onTabReselected(int position) {

    }
}
